﻿function ShowDownMenu(e) {
    $("#downmenu").show();
    var html = "", ismobiljiaemail = 2;
    var zh = $("#account").val();

    idele = -1;
    
    if (zh.indexOf('@') <= 0) { // || (zh.substr(0, 2) != "13" && zh.substr(0, 2) != "14" && zh.substr(0, 2) != "15" && zh.substr(0, 2) != "18")
        html += '<div class="row">' + zh.replace('@', '') + '@qq.com</div>';
        html += '<div class="row">' + zh.replace('@', '') + '@163.com</div>';
        html += '<div class="row">' + zh.replace('@', '') + '@126.com</div>';
        html += '<div class="row">' + zh.replace('@', '') + '@sohu.com</div>';
        html += '<div class="row">' + zh.replace('@', '') + '@sina.com</div>';
        html += '<div class="row">' + zh.replace('@', '') + '@vip.qq.com</div>';
        if (maxrow == 7) {
            html += '<div class="row">' + zh.replace('@', '') + '@foxmail.com</div>';
            html += '<div class="row">' + zh.replace('@', '') + '@gmail.com</div>';
        }
        $("#downmenu").show().empty().append(html);

        //绑定下拉框的鼠标事件
        $("#downmenu .row").hover(function () {
            $(this).parent().find("bluerow").removeClass("bluerow"); $(this).addClass("bluerow");
        }, function () {
            $(this).removeClass("bluerow");
        });

        $("#downmenu .row").click(function () {
            $("#account").val($(this).text());
            $(this).parent().hide();
            i = -1;
            isdelete = false;
        });
        ismobiljiaemail = -1;
    }
    else {
        //有@输入后就是找后缀的匹配而不是追加后缀

        //下拉列表的显示与否会随着输入而反复重置 每次都需要重置为都显示在控制那些隐藏 全部显示 重置最大值等
        $(".showR").removeClass("showR");
        $("#downmenu .row").show();
        maxrow = $("#downmenu .row").length - 1;
        isdelete = false; //是否删除过列表项 删除是将列表项目隐藏 class不同 所以需要变量存储是否隐藏过
        
        if (/\@$/.test(zh) == false) {
            $("#downmenu .row:contains('" + $("#account").val() + "')").addClass("showR");
            $("#downmenu .row").hide();
            $(".showR").show();
            isdelete = true;
            //重置最大下拉框个数
            maxrow = $(".showR").length - 1;
        }
    }
    if (maxrow < 0)
    {
        $("#downmenu").hide();
    }
    if (maxrow == 0)
    {
        //console.log($("#downmenu .row")[0].html);
    }
    var keyCode = e.keyCode || e.which;
    
    if (keyCode == 40) {
        //向下键
        if (i == -1) {
            i = 0;
        }
        else {
            i = i + 1;
        }

        if (i == (maxrow + 1)) {
            i = 0;
        }
        
        if (i > maxrow) {
            i = 0;
        }

        if (isdelete == true) {
            $("#downmenu").find(".bluerow").removeClass("bluerow");
            $("#downmenu .showR:eq(" + i + ")").addClass("bluerow");
        }
        else {
            $("#downmenu").find(".bluerow").removeClass("bluerow");
            $("#downmenu .row:eq(" + i + ")").addClass("bluerow");
        }
    }
    else if (keyCode == 38) {
        //向上键
        i = i - 1;
        if (i == -1) {
            i = maxrow;
        }
        if (i > maxrow) {
            i = 0;
        }

        if (isdelete == true) {
            $("#downmenu").find(".bluerow").removeClass("bluerow");
            $("#downmenu .showR:eq(" + i + ")").addClass("bluerow");
        }
        else {
            $("#downmenu").find(".bluerow").removeClass("bluerow");
            $("#downmenu .row:eq(" + i + ")").addClass("bluerow");
        }
    }
    else if (keyCode == 13) {
        //回车
        if (isdelete == true) {
            $("#account").val($("#downmenu .showR:eq(" + i + ")").text());
        }
        else {
            $("#account").val($("#downmenu .row:eq(" + i + ")").text());
        }
        $("#downmenu").hide();
        i = -1;
    }

    if (zh.length == 0) {
        $("#downmenu").hide();
    }
}